package com.example.group.network.express_plus_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SearchView;
import android.widget.Toast;


import com.example.group.network.express_plus_app.adapter.LinearAdapter;
import com.example.group.network.express_plus_app.bean.Order;
import com.example.group.network.express_plus_app.viewmodel.SearchViewModel;
import com.example.group.network.express_plus_app.widget.SpacesItemDecoration;
import com.xuexiang.xui.XUI;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

    private SearchViewModel searchViewModel;
    @BindView(R.id.rv_search_order_look)
    RecyclerView rv_search_order_look;
    @BindView(R.id.sv_search)
    SearchView sv_search;
    @BindView(R.id.rg_search)
    RadioGroup rg_search;
    @BindView(R.id.rb_express_address)
    RadioButton rb_express_address;
    @BindView(R.id.rb_home_address)
    RadioButton rb_home_address;
    @BindView(R.id.search_toolbar)
    Toolbar search_toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        searchViewModel=new ViewModelProvider(this).get(SearchViewModel.class);
        ButterKnife.bind(this);
        setSupportActionBar(search_toolbar);
        search_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        sv_search.setSubmitButtonEnabled(true);

        sv_search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //当点击搜索按钮时
                String param="快递地址搜索";
                if (rb_home_address.isChecked()){
                    param="配送地址搜索";
                }
                searchViewModel.findOrderBySearchAndParam(query,param);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        searchViewModel.getText().observe(this, new Observer<ArrayList<Order>>() {
            @Override
            public void onChanged(ArrayList<Order> orders) {
                //订单展示
                LinearLayoutManager manager=new LinearLayoutManager(getApplicationContext());
                manager.setOrientation(RecyclerView.VERTICAL);
                rv_search_order_look.setLayoutManager(manager);
                LinearAdapter adapter=
                        new LinearAdapter(getApplicationContext(), orders);
                adapter.setOnItemClickListener(adapter);
                rv_search_order_look.setAdapter(adapter);

            }
        });
        rv_search_order_look.setItemAnimator(new DefaultItemAnimator()); //设置列表项增删动画
        rv_search_order_look.addItemDecoration(new SpacesItemDecoration(6)); //设置列表项分割线
    }


    @Override
    public void onClick(View v) {

    }
}